//
//  LPActivityDetailStudyTableViewCell.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/9.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPActivityDetailStudyTableViewCell.h"
#import "LPActivityDetailTableFooterView.h"
#import "LPActivityDetailTableHeaderView.h"
/** 视频、音频cell */
#import "ZJStudyVideoTableViewCell.h"
/** 图书cell */
#import "ZJStudyBookTableViewCell.h"
#import "LPActivityDetailStudyNoDataTableViewCell.h"

@interface LPActivityDetailStudyTableViewCell ()<UITableViewDelegate,UITableViewDataSource,LPActivityDetailFooterDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  tableHEIGTH  */
@property (assign, nonatomic) CGFloat  tableHeight;

@end
@implementation LPActivityDetailStudyTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutUI];
    }
    return self;
}


- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    self.tableHeight = 0.0;
    
    if (modelArray.count != 0) {
        for (ZJStudyRecommendModel *model in modelArray) {
            if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
                self.tableHeight += 125 * [UIScreen zj_screenHScale];//图书
            }else{
                self.tableHeight += 120 * [UIScreen zj_screenHScale]; //视频、音频
            }
            
        }
        self.tableHeight += 110 * [UIScreen zj_screenHScale];
        CGRect frame = self.tableView.frame;
        frame.size.height = self.tableHeight;
        self.tableView.frame = frame;
        [self addFooterAndHeader];
        [self.tableView reloadData];
    }
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
}


#pragma mark - 私有方法
- (void)addFooterAndHeader{
    LPActivityDetailTableHeaderView *heaer = [[LPActivityDetailTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 50 * [UIScreen zj_screenHScale]) buttonTitle:@"学习" buttonImgae:@"wxy_xuexi_l"];
    self.tableView.tableHeaderView = heaer;
    
    LPActivityDetailTableFooterView *footer = [[LPActivityDetailTableFooterView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 60 * [UIScreen zj_screenHScale])];
    footer.titles = @[@"查看更多学习"];
    footer.delegate = self;
    self.tableView.tableFooterView = footer;
    
}

#pragma mark - LPActivityDetailFooterDelegate
- (void)lpActivityFooter_clickButton_index:(NSInteger)index{
    /**  查看更多的点击方法  */
    if (self.delegate) {
        [self.delegate LPActivityDetailStudyTableWithCell:self watchMore:YES];
    }
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.modelArray.count == 0) {
        return 1;
    }else{
        return self.modelArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    if (self.modelArray.count == 0) {
        LPActivityDetailStudyNoDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[LPActivityDetailStudyNoDataTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.title.text = @"暂无学习";
        return cell;
    }else{
        ZJStudyRecommendModel * model = self.modelArray[indexPath.row];
        if ([model.COURSETYPE isEqualToString:@"4"]) {//悦读会
            ZJStudyBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
            if (!cell) {
                cell = [[ZJStudyBookTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
            }
            cell.model = model;
            return cell;
        }else{
            ZJStudyVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
            if (!cell) {
                cell = [[ZJStudyVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
            }
            cell.model = model;
            return cell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.modelArray.count != 0) {
        ZJStudyRecommendModel *model = self.modelArray[indexPath.row];
        if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
            return 125 * [UIScreen zj_screenHScale];//图书
        }
        return 120 * [UIScreen zj_screenHScale];//视频、音频
    }else{
        return 80 * [UIScreen zj_screenHScale];
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.modelArray.count != 0) {
        if (self.delegate) {
            [self.delegate LPActivityDetailStudyTableWithCell:self index:indexPath.row];
        }
    }
    
}
#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 80) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}
@end
